<?php

declare(strict_types=1);

namespace App\Service\PageService;

use App\Exception\BusinessException;
use App\Service\DataService\ChannelDS;


class ChannelPS extends BasePS
{

    protected $ds;

    public function __construct(ChannelDS $ds)
    {
        $this->ds = $ds;
    }


    public function list(array $loginUserInfo, array $param, int $page, int $size): array
    {
        // 业务异常
//        throw new BusinessException(-2912,'我要抛出异常1');

        $param = unsetEmptyStringsData($param);

        $data = ['total' => 0, 'list' => []];

        $searchData = $this->makeListsSearchData($loginUserInfo, $param);

        $group = ['channel_type', 'channel_name'];

        $data['total'] = $this->ds->total($searchData, $group);
        if (!$data['total']) {
            return $data;
        }

        $fields = ['channel_id', 'channel_type', 'channel_name'];

        $data['list'] = $this->ds->list($searchData, $page, $size, ['created_at' => 'DESC', 'channel_id' => 'ASC'], $fields, $group);

        return $data;
    }

    protected function makeListsSearchData(array $loginUserInfo, array $param): array
    {
        $searchData['creator_uid'] = $loginUserInfo['uid'];

        isset($param['channelType']) && $searchData['channelType'] = $param['channelType'];

        return $searchData;
    }


}
